package com.bishe.cyh.spark;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.arcsoft.face.toolkit.ImageInfo;
import com.bishe.cyh.utils.SnowIdUtils;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;

import static com.arcsoft.face.toolkit.ImageFactory.getRGBData;

/**
 * @Author: Poppin
 * @Date: 2022/2/28 17:17
 * @Version: 1.0
 */
public class ArcFace {

    public static FaceEngine faceEngine = new FaceEngine("/home/fuck/libs/LINUX64");
    public static FunctionConfiguration configuration = new FunctionConfiguration();
    public static EngineConfiguration engineConfiguration = new EngineConfiguration();
    public static int errorCode;
    public static int initCode = 0;
    public static float threshold1;
    public static List<FaceEnity> faceEnities = new ArrayList<FaceEnity>();


    public static void init(String tablename,float threshold){
        threshold1 = threshold;
        String appId = "FHoVsWDG7GYyo8AugpDGPFYb8zTdummL8uJSijASq1id";
        String sdkKey = "Bi26TPmNaA934XZEJsPv87RS7YH415oFQDB1z4Xw3UoY";
        errorCode = faceEngine.activeOnline(appId,sdkKey);
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);
        configuration.setSupportFaceDetect(true);
        configuration.setSupportFaceRecognition(true);
        configuration.setSupportAge(true);
        configuration.setSupportGender(true);
        configuration.setSupportFace3dAngle(true);
        configuration.setSupportLiveness(true);
        configuration.setSupportIRLiveness(true);
        engineConfiguration.setFunctionConfiguration(configuration);
        errorCode=faceEngine.init(engineConfiguration);
        if (errorCode!= ErrorInfo.MOK.getValue()){
            initCode++;
            init(tablename,threshold);
        }
        HBaseClient.init(tablename);
    }

    public static void JC(byte[] image,String Name){
        ImageInfo imageInfo = getRGBData(image);
        List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
        errorCode = faceEngine.detectFaces(imageInfo.getImageData(),imageInfo.getWidth(),imageInfo.getHeight(),imageInfo.getImageFormat(),faceInfoList);
        if (faceInfoList.isEmpty()){
            return;
        }
        FaceFeature faceFeature = new FaceFeature();
        errorCode = faceEngine.extractFaceFeature(imageInfo.getImageData(),imageInfo.getWidth(),imageInfo.getHeight(),imageInfo.getImageFormat(),faceInfoList.get(0),faceFeature);

        if (errorCode==0){
            HBaseClient.addRecord(SnowIdUtils.uniqueLongHex(),faceFeature.getFeatureData());
        }
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        functionConfiguration.setSupportAge(true);
        functionConfiguration.setSupportGender(true);

        /**
         * 人脸属性检测
         */
        errorCode = faceEngine.process(imageInfo.getImageData(),imageInfo.getWidth(),imageInfo.getHeight(),imageInfo.getImageFormat(),faceInfoList,functionConfiguration);

        /**
         * 性别检测
         */
        List<GenderInfo> genderInfoList = new ArrayList<GenderInfo>();
        errorCode = faceEngine.getGender(genderInfoList);

        /**
         * 年龄检测
         */
        List<AgeInfo> ageInfoList = new ArrayList<AgeInfo>();
        errorCode = faceEngine.getAge(ageInfoList);
        HBaseClient.addData(SnowIdUtils.uniqueLongHex(),ageInfoList.get(0).getAge(),genderInfoList.get(0).getGender());

    }


    public static void Computer(byte[] image,String Name){
        FaceFeature sourceFaceFeature = new FaceFeature();
        sourceFaceFeature.setFeatureData(image);
        for (FaceEnity faceEnity:faceEnities){
            if (Name.equals(faceEnity.getName())){
                continue;
            }
            FaceFeature targetFaceFeature = new FaceFeature();
            targetFaceFeature.setFeatureData(faceEnity.getFaceFeature());
            FaceSimilar faceSimilar = new FaceSimilar();
            errorCode = faceEngine.compareFaceFeature(sourceFaceFeature,targetFaceFeature,faceSimilar);
            if (faceSimilar.getScore()>=threshold1){
                HBaseClient.add(SnowIdUtils.uniqueLongHex(),"success");
                return;
            }
        }
    }

    public static void GetFaceFeature(){
        if (HBaseClient.FaceAll()!=null){
            faceEnities=HBaseClient.FaceAll();
        }else {
            GetFaceFeature();
        }
    }

    public static void unit(){
        errorCode=faceEngine.unInit();
        HBaseClient.close();
    }
}
